package com.cuitrip.business.home.favorite;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.business.home.IndexActivity;
import com.cuitrip.business.home.favorite.proxy.FavProxy;
import com.cuitrip.business.tripservice.ServiceDetailActivity;
import com.cuitrip.business.login.LoginInstance;
import com.cuitrip.component.choicedialog.ChoiceDialog;
import com.cuitrip.component.statelayout.StateLayout;
import com.cuitrip.component.verticallist.BaseVerticalListFragment;
import com.cuitrip.service.R;
import com.cuitrip.util.track.TrackUtil;
import com.lab.utils.MessageUtils;

import java.util.List;

import butterknife.Bind;
import jp.wasabeef.recyclerview.animators.SlideInRightAnimator;

/*
 * 喜欢列表
 * Created by ak
 */
public class FavoriteListFragment extends BaseVerticalListFragment<TravelTrip> implements IFavoriteListView {

    public static final String TAG = "FavoriteListFragment";

    public static final String FAV_CHANGED_ACTION = "fav_changed";
    public static final String FAV_CHANGED_VALUE_KEY = "key_fav_value";

    @Bind(R.id.state_layout)
    StateLayout stateLayout;

    @Bind(R.id.top_bar)
    View topBar;

    @Bind(R.id.bar_middle_item)
    TextView middleItem;


    private View.OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ct_service:
                    if (v.getTag() != null && v.getTag() instanceof TravelTrip) {
                        TravelTrip messageMode = (TravelTrip) v.getTag();
                        mFavoritePresent.onClick(messageMode);
                    }
                    break;
                case R.id.ct_delete:
                    if (v.getTag() != null && v.getTag() instanceof TravelTrip) {
                        TravelTrip messageMode = (TravelTrip) v.getTag();
                        mFavoritePresent.onMove(messageMode);
                    }
                    break;
                case R.id.like_img:
                    if (v.getTag() != null && v.getTag() instanceof TravelTrip) {
                        final TravelTrip messageMode = (TravelTrip) v.getTag();

                        ChoiceDialog choiceDialog = ChoiceDialog.getInstance();
                        choiceDialog.setDialogTitleVisibility(false);
                        choiceDialog.setDialogMessage(getResources().getString(R.string.favorite_operation_delete_confirm));
                        choiceDialog.setDialogCoupleStyleSetting(
                                getResources().getString(R.string.operation_yes),
                                getResources().getString(R.string.operation_cancel), new ChoiceDialog.DialogStyleCoupleCallback() {
                                    @Override
                                    public boolean onPositiveButtonClick() {
                                        FavProxy.getInstance().unFavTrip(messageMode.getSid(), new FavProxy.Callback() {
                                            @Override
                                            public void onSuccess() {
                                                mFavoritePresent.onMove(messageMode);
                                            }

                                            @Override
                                            public void onFail(String message) {
                                                MessageUtils.showToast(message);
                                            }
                                        });
                                        return false;
                                    }

                                    @Override
                                    public boolean onNegativeButtonClick() {
                                        return false;
                                    }
                                });
                        getHostActivity().showDialogFragment(choiceDialog);


                    }
                    break;
                default:
                    break;
            }
        }
    };
    private FavoritePresent<TravelTrip> mFavoritePresent = new FavoritePresent<>(this);
    private FavoriteAdapter mAdapter = new FavoriteAdapter(mOnClickListener);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private View mContentView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = super.onCreateView(inflater, container, savedInstanceState);
        IntentFilter filter = new IntentFilter(FavoriteListFragment.FAV_CHANGED_ACTION);
        getContext().registerReceiver(mReceiver, filter);
        initListener();
        mRecyclerView.setAdapter(mAdapter);
        SlideInRightAnimator animator = new SlideInRightAnimator();
        mRecyclerView.setItemAnimator(animator);
        stateLayout.changeState(StateLayout.State.Loading);
        requestPresentRefresh();
        initData();
        return mContentView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        getContext().unregisterReceiver(mReceiver);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initListener();
        updateStateLayout();
    }

    private void initData() {
        if (LoginInstance.isLogin()) {
            topBar.setBackgroundColor(getResources().getColor(R.color.ct_white));
            middleItem.setTextColor(getResources().getColor(R.color.ct_black));
            middleItem.setText(getString(R.string.main_nav_favorites));
        }
    }

    private void initListener(){
        stateLayout.setAction(new StateLayout.Action() {
            @Override
            public void performEmptyAction() {
                IndexActivity.gotoTab(getHostActivity(),IndexActivity.RECOMMEND_TAB_INDEX);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        TrackUtil.trackScreenName("/user/favorites");
    }

    @Override
    protected int layoutRes() {
        return R.layout.favorite_list;
    }

    @Override
    public void requestPresentLoadMore() {
        uiShowLoadMore();
        mFavoritePresent.requestLoadMore();
    }

    @Override
    public void requestPresentRefresh() {
        if (LoginInstance.isLogin()) {
            uiHideLoadMore();
            mFavoritePresent.requestRefresh();
        } else {
            stateLayout.changeState(StateLayout.State.Empty);
        }
    }

    @Override
    public void renderUIWithAppendData(List items) {
        mAdapter.appendModeList(items);
        updateStateLayout();
    }

    private void updateStateLayout() {
        if (!isAdded()){
            return;
        }
        if (mAdapter.getItemCount() == 0) {
            stateLayout.changeState(StateLayout.State.Empty);
        } else {
            stateLayout.changeState(StateLayout.State.INIT);
        }
    }

    @Override
    public void renderUIWithData(List items) {
        mAdapter.setModeList(items);
        updateStateLayout();
    }

    @Override
    public void jump(TravelTrip messageMode) {
        ServiceDetailActivity.start(getActivity(), messageMode.getSid());
    }

    @Override
    public void delete(TravelTrip messageMode) {
        mAdapter.remove(messageMode);
        updateStateLayout();
    }

    @Override
    public void jumpUnvaliable(TravelTrip mode) {
        FavorityUnvaliableActivity.start(getActivity(), mode.getServicePicUrl());

    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            requestPresentRefresh();
        }
    };

    @Override
    public int getSize() {
        return mAdapter.getItemCount();
    }
}
